import React from 'react';
import { inject, observer } from 'mobx-react';
import { Spin, Table, Button, Modal, Tooltip, Row, Col } from 'antd';
import NewLinkeb from '../../../public/newlinked/NewLinkeb';
import { WeaUpload, WeaLocaleProvider } from 'ecCom';
import * as Util from '../../../util/Util.js';
const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;

@inject('planReportViewStore')
@observer
export default class RelatedFiles extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      operation: 'save'
    };
    this.store = this.props.planReportViewStore
  }

  componentDidMount() {
    this.props.refFile(this)
  }

  doUpload = (operation) => {
    this.refs.addgoalupload.doUpload();

    this.store.loading = true
    this.setState({ operation })
  }

  /**上传附件 */
  uploadChange(ids, list) {
    this.store.uploadReset = false
    let resData = toJS(this.store.resData)
    if (ids.length > 0) {
      let newAddFileids = '';
      for (let i = 0; i < list.length; i++) {
        let fileA = {};
        if (resData.fileids.indexOf(',' + ids[i] + ',') == -1) {
          newAddFileids += ids[i] + ',';
          resData.fileids += ids[i] + ',';
          fileA.id = ids[i];
          fileA.name = list[i].filename;
          resData.filearry.push(fileA);
        }
      }
    }
    this.store.resData = resData
    this.store.loading = false
    this.store.uploading = false
    this.store.doOperation(this.state.operation)
    this.store.uploadReset = true
  }

  startWith(str, dim) {
    var reg = new RegExp("^" + dim);
    return reg.test(str);
  }
  endWith(str, dim) {
    var reg = new RegExp(dim + "$");
    return reg.test(str);
  }

  onLinkClick(id, name, type, typeIds, typeArray, fileid) {
    let resData = toJS(this.store.resData)
    let hasIdsArray = [];
    if (type == 2) {
      //对array进行处理
      if (typeIds != "") {
        let vals = resData[typeIds];
        if (vals.length > 0 && !this.startWith(vals, ',')) {
          vals = ',' + vals;
        }
        if (vals.length > 0 && !this.endWith(vals, ',')) {
          vals = vals + ',';
        }
        let _index = vals.indexOf("," + id + ",")
        if (_index > -1 && id != "") {
          vals = vals.substring(0, _index + 1) + vals.substring(_index + (id + "").length + 2);
          if (vals == ',') {
            vals = '';
          }
          resData[typeIds] = vals;
        }
      } else {
        let hasArray = resData[typeArray];
        for (let j = 0; j < hasArray.length; j++) {
          hasIdsArray.push(hasArray[j].id);
        }
        if (this.IsInArray(hasIdsArray, id) != -1) {
          hasIdsArray.splice(hasIdsArray.indexOf(id), 1);
        }
      }
      let array = resData[typeArray];
      array.map((element, index) => {
        if (element.id == id) {
          resData[typeArray].splice(index, 1);
        }
      });

    } else if (type == 1) {
      if (typeIds == "fileids") {
        this.store.viewDocFiled({
          id: id, relateId: this.store.resData.planid, type: 4, doback: function () {
            Util.opendoc(id);
          }
        });
      }
    } else {
      this.store.viewDocFiled({
        id: id, relateId: this.store.resData.planid, type: 4, doback: function () {
          Util.downloads(fileid);
        }
      });
    }
    this.store.refreshResData(resData)
  }
  setUploading(listB) {
    if (listB.length > 0) {
      this.store.uploading = true;
    } else {
      this.store.uploading = false;
    }
  }
  render() {
    const { resData, fileComLoading  } = this.store
    const { filearry } = resData
    return <div className="relate-file">
      {/* {this.store.resData.filearry.length<1 && '暂无'} */}
      <span className="line-span"><NewLinkeb ecId={`${this && this.props && this.props.ecId || ''}_NewLinkeb@s6ezsq`} onLinkClick={(id, name, type, fileid) => this.onLinkClick(id, name, type, "fileids", 'filearry', fileid)} linkurl='' linkdatas={filearry} hasdownload={true} hasdel={this.store.buttonAuth.edit} /></span>
      {this.store.buttonAuth.edit && this.store.uploadReset && !fileComLoading && 
        <div className="upload-area">
          <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@o8yopz`} uploadUrl={'/api/doc/upload/uploadFile2Doc?category=' + resData.category} category={resData.category ? resData.category : ''}
            viewAttr='1' showClearAll={false} autoUpload={false} maxUploadSize={resData.maxsize}
            getShowListDatas={(listT, listB) => this.setUploading(listB)}
            datas={[]}
            onChange={(ids, list) => this.uploadChange(ids, list)} ref={'addgoalupload'}
          />

        </div>
      }
    </div>
  }
}
